package cn.wk.libs4a.utils.camera;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import uk.co.senab.photoview.PhotoView;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import cn.wk.libs4a.R;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.WKBaseActivity;

import com.itotem.imageloader.core.assist.FailReason;
import com.itotem.imageloader.core.assist.ImageLoadingListener;

public class ImageZoomActivity extends WKBaseActivity implements OnClickListener {

	private PhotoView img;
	private View close;
	private View save;
	private Bitmap bitmap;
	private View progress;
	private String uri;
	private int type;
	protected int width;
	protected int height;

	@Override
	public void loadXml() {
		setContentView(R.layout.zoom);
	}

	@Override
	public void init() {
		uri = getIntent().getStringExtra("uri");
		type = getIntent().getIntExtra("type", 0);
		img = (PhotoView) findViewById(R.id.photo);
		close = findViewById(R.id.zoom_close);
		close.setOnClickListener(this);
		save = findViewById(R.id.zoom_save);
		save.setOnClickListener(this);
		progress = findViewById(R.id.progress);

	}

	@Override
	public void setData() {
		if (uri == null) {
			return;
		}
		save.setClickable(false);
		app().imageLoader.displayImage(uri, img, app().options,
				new ImageLoadingListener() {

					@Override
					public void onLoadingStarted() {

					}

					@Override
					public void onLoadingFailed(FailReason failReason) {

					}

					@Override
					public void onLoadingComplete(Bitmap loadedImage) {
						long size = new File(uri).length();
						double max = 2 * 1024 * 1024;
						if (size > max) {
							float scale = (float) (max / size - 0.1);
							WKApplication.showMsg("文件大于2m：" + scale);
							try {
								loadedImage.compress(
										CompressFormat.JPEG,
										(int) (100 * scale),
										new FileOutputStream(app().imageLoader
												.getFilePath(uri)));
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
						}
						width = loadedImage.getWidth();
						height = loadedImage.getHeight();
						progress.setVisibility(View.GONE);
						img.setClickable(true);
						save.setClickable(true);
					}

					@Override
					public void onLoadingCancelled() {
					}
				});
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.zoom_close) {
			onBackPressed();
		} else if (id == R.id.zoom_save) {
			Intent intent = new Intent();
			intent.putExtra("uri", uri);
			intent.putExtra("width", width);
			intent.putExtra("height", height);
			setResult(RESULT_OK, intent);
			finish();
		} else {
		}
	}

	/**
	 * 保存到本地文件
	 */
	private void saveLocal() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			saveSD(Environment.getExternalStorageDirectory() + "/affairs");
		} else {
			WKApplication.showMsg("已保存到");
		}
	}

	/**
	 * 
	 * @param string
	 *            from
	 * @param str
	 *            to
	 */
	private void saveSD(String path) {
		File fileTo = new File(path);
		if (!fileTo.exists()) {
			fileTo.mkdirs();
		}
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(new File(fileTo,
					System.currentTimeMillis() + ".png"));
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
			WKApplication.showMsg("已保存到本地");
		} catch (Exception e) {
			e.printStackTrace();
			WKApplication.showMsg("保存异常");
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (img != null) {
			img.recycle();
		}
	}
}
